﻿using System;
using System.Windows.Forms;

using Utility;

namespace BeatFinder
{
    public partial class MainForm : Form
    {
        uint sampleCount = 0;
        TimeSpan avgTime;
        TimeSpan totalTime = TimeSpan.FromTicks(0L);
        DateTime prevTime;

        public MainForm()
        {
            InitializeComponent();
        }

        private void btnBeat_Click(object sender, EventArgs e)
        {
            if (sampleCount++ == 0)
            {
                prevTime = DateTime.Now;
                return;
            }

            DateTime now = DateTime.Now;
            TimeSpan deltaTime = now - prevTime;
            totalTime += deltaTime;
            prevTime = now;
            avgTime = totalTime.Multiply(1.0 / (float)(sampleCount - 1));

            double bpm = 1.0 / avgTime.TotalMinutes;
            btnBeat.Text = string.Format("{0:0} BPM", bpm);
            Text = string.Format("BeatFinder: {0:0.00} BPM {1} samples", bpm, sampleCount);
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            sampleCount = 0;
            totalTime = TimeSpan.FromTicks(0L);
            btnBeat.Text = "0 BPM";
            Text = "BeatFinder";
        }
    }
}
